# test.py
import sys
import os
# 将当前文件所在目录的父目录添加到Python路径中
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

import pytest
import allure
import logging
from time import sleep
from pages.login_page import LoginPage

logger = logging.getLogger(__name__)

@allure.feature("购物商城登录测试")  #定义测试用例的功能模块
class TestLogin:

    @allure.story("成功登录场景")
    @allure.title("测试正确的用户名和密码")
    @allure.severity(allure.severity_level.CRITICAL)  #优先级
    def test_shopping_mall_001_case1(self, driver, reset_page, setup_test_data): 
        """测试正确的用户名和密码"""
        with allure.step("初始化登录页面"):  #在allure中，进入with块Allure自动开始记录该步骤，离开with块结束记录
            login_page = LoginPage(driver)
        
        with allure.step("输入正确的用户名和密码"):
            login_page.login('20171111111', '111111')
            sleep(1)
        
        with allure.step("验证欢迎消息"):
            welcome_message = login_page.get_welcome_message()
            allure.attach(f"实际欢迎消息: {welcome_message}", name="欢迎消息")
            assert welcome_message == "欢迎您，李四"
        
        with allure.step("退出登录"):
            login_page.logout()
            logger.info("登录成功，断言通过")

    @allure.story("失败登录场景")
    @allure.title("测试错误的学号")
    @allure.severity(allure.severity_level.NORMAL)
    def test_shopping_mall_001_case2(self, driver, reset_page, setup_test_data):
        """测试错误的学号"""
        with allure.step("初始化登录页面"):
            login_page = LoginPage(driver)
        
        with allure.step("输入错误的学号"):
            login_page.login('20173111111', '111111')
            sleep(1)
        
        with allure.step("验证错误消息"):
            error_message = login_page.get_error_message()
            allure.attach(f"实际错误消息: {error_message}", name="错误消息")
            assert error_message == '学号错误'
            logger.info("学号错误，断言通过")

    @allure.story("失败登录场景")
    @allure.title("测试错误的密码")
    @allure.severity(allure.severity_level.NORMAL)
    def test_shopping_mall_001_case3(self, driver, reset_page, setup_test_data):
        """测试错误的密码"""
        with allure.step("初始化登录页面"):
            login_page = LoginPage(driver)
        
        with allure.step("输入错误的密码"):
            login_page.login('20171111111', '12666666')
            sleep(1)
        
        with allure.step("验证错误消息"):
            error_message = login_page.get_error_message()
            allure.attach(f"实际错误消息: {error_message}", name="错误消息")
            assert error_message == '密码错误'
            logger.info("密码错误，断言通过")
@allure.feature("商品搜索测试")  # 使用独特的feature名称
class TestSearch:
    @allure.story("成功搜索场景")
    @allure.title("测试能否搜索成功")
    @allure.severity(allure.severity_level.CRITICAL)  #优先级
    def test_search_001_case1(self, driver, reset_page, setup_test_data): 
        """测试搜索商品成功"""
        with allure.step("初始化登录页面"):  #在allure中，进入with块Allure自动开始记录该步骤，离开with块结束记录
            login_page = LoginPage(driver)
        
        with allure.step("输入商品名"):
            login_page.research('追风筝的人')
            sleep(1)
        
        with allure.step("验证搜索成功"):
            shangpin_message = login_page.get_shangpin_message()
            allure.attach(f"搜索结果： {shangpin_message}")
            assert shangpin_message == "追风筝的人"
        
        with allure.step("退出登录"):
            logger.info("搜索成功，断言通过")

    @allure.story("失败搜索场景")
    @allure.title("测试搜索不存在的商品")
    @allure.severity(allure.severity_level.NORMAL)
    def test_search_001_case2(self, driver, reset_page, setup_test_data):
        """测试搜索商品失败"""
        with allure.step("初始化登录页面"):
            login_page = LoginPage(driver)
        
        with allure.step("输入商品名"):
            login_page.research('追鱼')
            sleep(1)
        
        with allure.step("验证搜索失败"):
            shangpin_message = login_page.get_error_shangpin()
            allure.attach(f"搜索结果： {shangpin_message}")
            assert shangpin_message == "抱歉，没有找到相关商品"
        
        with allure.step("退出登录"):
            logger.info("搜索失败，断言通过")
